package com.pets.controller;


import com.pets.entity.Users;
import com.pets.service.ICollectService;
import com.pets.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏表接口")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    @ApiOperation("添加收藏")
    @GetMapping
    public Result register(@ApiParam(value = "文件id", required = true)@RequestParam Integer fileId,
                           @ApiParam(value = "用户id", required = true)@RequestParam Integer userId,
                           HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            return collectService.register(fileId,user.getUserId());
        }else {
            return Result.fail("未登录！");
        }*/

        return collectService.register(fileId,userId);
    }


    @ApiOperation("移除收藏")
    @DeleteMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "收藏id", required = true)@PathVariable Integer id) {
        //软删除
        return collectService.deleteById(id);
    }

    @ApiOperation("收藏分页查询")
    @GetMapping("/listPage")
    public Result listPage(@ApiParam(value = "页码", required = true)@RequestParam(defaultValue = "1") Integer page,
                           @ApiParam(value = "页数", required = true)@RequestParam(defaultValue = "10") Integer pageSize,
                           @ApiParam(value = "文件名称")@RequestParam(required = false) String fileName,
                           @ApiParam(value = "用户id")@RequestParam(required = false) Integer userId,
                           HttpSession session) {
        /*Object userObject = session.getAttribute("users");
        if (userObject instanceof Users) {
            Users user = (Users) userObject;
            System.out.println(user.getUsername()+"/"+user.getUserId());
            return collectService.selectPage(page, pageSize, fileName,user.getUserId());
        }else {
            return Result.fail("未登录！");
        }*/

        return collectService.selectPage(page, pageSize, fileName,userId);
    }
}
